package my.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import my.db.Academy;
import my.db.Clazz;
import my.db.ClazzCourse;
import my.db.Student;
import my.db.Teacher;
import my.tool.MybatisApi;

@Controller
public class ClazzController {

	@GetMapping("clazz")
	public String ClazzPage(Model model,HttpServletRequest request) {
//		查询，所有学院，所有教师
		List<Academy> AcademyList = MybatisApi.selectList("zhy.sesClazz.getAcademyTable", Academy.class);
		List<Academy> academyList = MybatisApi.selectList("zhy.sesClazz.getAcademys", Academy.class);
		List<Teacher> TeacherList = MybatisApi.selectList("zhy.sesClazz.getTeachers", Teacher.class);
		List<Clazz> ClazzList = MybatisApi.selectList("zhy.sesClazz.getClazzs", Clazz.class);
		List<Map<String, Object>> A_TeacherList = MybatisApi.selectList("zhy.sesClazz.getAcademy_Teacher", 0);
		List<Map<String, Object>> A_T_ClazzList = MybatisApi.selectList("zhy.sesClazz.getClazzAll", 0);
		
		
		model.addAttribute("TeacherList",TeacherList);
		model.addAttribute("AcademyList", AcademyList);
		model.addAttribute("academyList", academyList);
		model.addAttribute("A_TeacherList", A_TeacherList);
		model.addAttribute("ClazzList", ClazzList);
		model.addAttribute("A_T_ClazzList", A_T_ClazzList);
		model.addAttribute("datas", IndexController.SimpleJS(request));
		return "Clazz_Manager";
	}
//	保存学院信息（添加一个学院）
	@PostMapping("clazz_saveAcademy.do")
	public Object saveAcademy(@RequestBody JSONObject jreq) {
		
		Academy academy = new Academy();
		academy.setAcademyName(jreq.getString("academyName"));
		academy.setAcademyId(jreq.getInteger("academyId"));
		academy.setDean(jreq.getInteger("teacherId"));
		
		Teacher teacher= new Teacher();
		teacher.setTeacherId(jreq.getInteger("teacherId"));
		teacher.setAcademyId(jreq.getInteger("academyId"));
		teacher.setIsDean(true);
		
		MybatisApi.insert("zhy.sesClazz.saveAcademy", academy);
		MybatisApi.update("zhy.sesClazz.upTeacherAid", teacher);
		return new AfRestData("");
		}
	
//	保存班级信息（添加一个班级）
	@PostMapping("clazz_savaClazz.do")
	public Object saveClazz(@RequestBody JSONObject jreq) {
		
		Clazz clazz = new Clazz();
		clazz.setAcademyId(jreq.getInteger("academyId"));
		clazz.setClazzId(jreq.getInteger("clazzId"));
		clazz.setInstructor(jreq.getInteger("teacherId"));
		clazz.setIsCourse(false);
		
		MybatisApi.insert("zhy.sesClazz.saveClazz", clazz);
		return new AfRestData("");
	}
	
//	保存修改后的学院管理信息
	@PostMapping("clazz_savaUpdateAcademy.do")
	public Object saveUpdateAcademy(@RequestBody JSONArray jreqs) {
		Teacher teacher = new Teacher();
		Teacher teacherold = new Teacher();
		for(int i=0;i<jreqs.size();i++) {
			Academy academy = jreqs.getObject(i, Academy.class);
			teacher.setAcademyId(academy.getAcademyId());
			teacher.setTeacherId(academy.getDean());
			teacher.setIsDean(true);
			
			if(academy.getOldDean() != null &&  academy.getOldDean() != 0) {
				teacherold.setAcademyId(academy.getAcademyId());
				teacherold.setTeacherId(academy.getOldDean());
				teacherold.setIsDean(false);
				MybatisApi.update("zhy.sesClazz.upTeacherAid", teacherold);
			}
			
			MybatisApi.update("zhy.sesClazz.saveUpdateAcademy", academy);
			MybatisApi.update("zhy.sesClazz.upTeacherAid", teacher);
		}
		return new AfRestData("");
	}
	
//	保存修改后的班级管理信息
	@PostMapping("clazz_savaUpdateClazz.do")
	public Object saveUpdateClazz(@RequestBody JSONArray jreqs) {
		
		for(int i=0;i<jreqs.size();i++) {
			Clazz clazz = new Clazz();
			clazz.setId(jreqs.getJSONObject(i).getInteger("id"));
			clazz.setClazzId(jreqs.getJSONObject(i).getInteger("ClazzId"));
			clazz.setAcademyId(jreqs.getJSONObject(i).getInteger("AcademyId"));
			clazz.setInstructor(jreqs.getJSONObject(i).getInteger("Instructor"));
			MybatisApi.update("zhy.sesClazz.saveUpdateClazz", clazz);
		}
		return new AfRestData("");
	}
	
//	删除一个班级信息
	@PostMapping("clazz_deleteClazz.do")
	public Object deleteClazz(@RequestBody JSONArray jreqs) {
		

			for(int i = 0;i<jreqs.size();i++) {
				
				MybatisApi.delete("zhy.sesClazz.deleteClazz", jreqs.get(i));
			}
		return new AfRestData("");
	}
	
	
}
